//
//  Draw_a_view.m
//  Metal-Master
//
//  Created by 杨冰冰 on 2020/6/15.
//  Copyright © 2020 杨冰冰. All rights reserved.
//

#import "Draw_a_view.h"
#import "DrawViewRenderer.h"

@implementation Draw_a_view
{
    MTKView *_view;
    DrawViewRenderer *_renderer;
}
- (void)viewDidLoad {
    _view = [[MTKView alloc] initWithFrame:self.bounds];
    
    [self addSubview:_view];
    //如果图像不需要构建动画，就设置SetNeedsDisplay为YES，只会调用1次即可。
    //如果不设置为YES，则默认为每秒60帧的速度调用刷新
    _view.enableSetNeedsDisplay = YES;
    
    _view.device = MTLCreateSystemDefaultDevice();
    
    _view.clearColor = MTLClearColorMake(0, 0.5, 1.0, 1.0);
    
    _renderer = [[DrawViewRenderer alloc] initWithMetalKitView:_view];
    
    if (!_renderer) {
        NSLog(@"Renderer initialization failed");
        return;
    }
    
    // Initialize the renderer with the view size.
    [_renderer mtkView:_view drawableSizeWillChange:_view.drawableSize];
    
    _view.delegate = _renderer;
}

@end
